package com.bdqn.qnnews.wemedia.controller.v1;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.bdqn.qnnews.common.base.PageResult;
import com.bdqn.qnnews.common.base.R;
import com.bdqn.qnnews.rest.exception.ApplicationException;
import com.bdqn.qnnews.wemedia.entity.WmNews;
import com.bdqn.qnnews.wemedia.service.WmNewsAutoScanService;
import com.bdqn.qnnews.wemedia.service.WmNewsService;
import com.bdqn.qnnews.wemedia.vo.AdminNewsListVo;
import com.bdqn.qnnews.wemedia.vo.WmNewsPageReqVo;
import com.bdqn.qnnews.wemedia.vo.WmNewsVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author xlz
 * @since 2023-12-26
 * @description 自媒体图文内容信息表
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/news")
@Tag(name = "自媒体图文内容信息表控制器类",
        description = "WmNewsController 自媒体图文内容信息表 后端数据接口")
public class WmNewsController {

    private final WmNewsService wmNewsService;
    private final WmNewsAutoScanService newsAutoScanService;


    /**
     * 自媒体端文章分页查询
     * @param newsPageReqVo 查询参数
     * @return R
     */
    @Operation(summary = "自媒体端文章分页查询", description = "自媒体端文章分页查询")
    @PostMapping("/list")
    public PageResult<List<WmNews>> getWmNewsPage(@RequestBody WmNewsPageReqVo newsPageReqVo) {
        return wmNewsService.findList(newsPageReqVo);
    }

    /**
     * 后台管理端文章分页查询
     * @param adminNewsListVo 查询参数
     * @return R
     */
    @Operation(summary = "后台管理端文章分页查询", description = "后台管理端文章分页查询")
    @PostMapping("/list_vo")
    public PageResult<List<WmNews>> getAdminNewsPage(@RequestBody AdminNewsListVo adminNewsListVo) {
        return wmNewsService.findAdminNewsList(adminNewsListVo);
    }

    /**
     * 发布文章
     */
    @Operation(summary = "文章发布", description = "发布文章，关联素材表")
    @PostMapping("/submit")
    public R<String> submit(@RequestBody WmNewsVo vo){
        // 前端发送的日期有8个小时的差，需要进行处理
        vo.setPublishTime(LocalDateTimeUtil.offset(vo.getPublishTime(),8, ChronoUnit.HOURS));
        // 判断是否发送成功
        final WmNews wmNews = wmNewsService.submit(vo);
        if(wmNews == null){
            throw new ApplicationException("文章创建失败");
        }

        // 默认发起文章的审核功能
        // 先自动审核(云平台都有内容安全服务)，如果自动审核失败，再进行人工审核
        newsAutoScanService.autoScanWmNews(wmNews.getId());

        return R.ok();
    }


    /**
     * 修改回显文章
     */
    @Operation(summary = "修改回显", description = "根据文章id查询要修改的文章内容")
    @GetMapping("/one/{id}")
    public R<WmNews> getById(@PathVariable("id") Long id) {
        final WmNews wmNews = wmNewsService.getById(id);
        return R.ok(wmNews);
    }

    /**
     * 后台管理端查看详情接口
     */
    @Operation(summary = "后台管理端查看详情接口", description = "根据文章id查询要人工审核的文章内容")
    @GetMapping("/one_vo/{id}")
    public R<WmNews> getAdminNewsById(@PathVariable("id") Long id) {
        final WmNews wmNews = wmNewsService.getById(id);
        return R.ok(wmNews);
    }



    /**
     * 通过id删除 自媒体图文内容信息表
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除自媒体图文内容信息表",
               description = "通过id删除自媒体图文内容信息表")
    @GetMapping("/del_news/{id}")
    public R<String> removeById(@PathVariable Long id) {
        Boolean res = wmNewsService.deleteNewsById(id);
        return R.ok("删除成功");
    }

}
